import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 * Single instance of the land office. Contains inventory information
 * and also has the ability to buy/sell land to players.
 * 
 * @author GT Five-O
 * @version v1.0b
 *
 */
public class LandOffice implements java.io.Serializable {
    private Random rand = new Random();
    private int inventory;
    private GameScreen board;
    private int buyPrice;
    private static boolean priceCalc = false;
    private static int price = -1;
    private static boolean landSelection = true;

    /**
     * Class constructor specifying the game board.
     * 
     * @param board
     *            the game board
     */
    public LandOffice(GameScreen board) {
        this.board = board;
    }

    /**
     * Updates inventory count.
     * 
     * @param num New inventory count.
     * @return Updated inventory count.
     */
    public int getNumInventory(int num) {
        inventory = num;
        return inventory;

    }

    /**
     * Calculates the price of a single piece of land.
     * 
     * @return Final price of the land being sold.
     */
    public int calcSell() {
        price = GameConstants.LAND_BUYING_PRICE
                + (board.getCurrent().getRound()) * rand.nextInt(101);
        return price;
    }

    /**
     * Get price of the land.
     * 
     * @return The price.
     */
    public int getSell() {
        return price;
    }

    /**
     * Sets whether or not the price has been calculated. By default this should
     * be set to false after every round in GameEngine.
     * 
     * @param b
     *            False if the price has not been calculated.
     */
    public void setCalculated(boolean b) {
        /** make sure to setCalculated to false after every round in GameEngine */
        priceCalc = b;
    }

    /**
     * Getter for the boolean variable priceCalc.
     * 
     * @return Boolean that states whether the price has been
     *         calculated or not.
     */
    public boolean calculated() {
        return priceCalc;
    }

    /**
     * Calculates price if it hasn't been done so yet and compares it to the
     * amount of money the player has.
     * 
     * @return True if the player has enough money to buy the land.
     */
    public boolean checkSell() {
        if (calculated()) {
            buyPrice = price;
        } else {
            buyPrice = calcSell();
        }
        Player current = board.getCurrent();
        if (current.affordable(buyPrice)) {
            return true;
        } else {
            JOptionPane.showMessageDialog(board,
                    "You Don't Have Enough Money! Sorry!");
            return false;
        }
    }

    /**
     * Runs the method buyProperty based on the price of the land.
     */
    public void sell() {
        Player current = board.getCurrent();
        current.buyProperty(buyPrice);
    }

    /**
     * Helper method to set the variable "landSelection" to false.
     */
    public void endThread() {
        landSelection = false;
    }

    /**
     * First prompts the user to buy property and then allows him to select his
     * land. The land selected is marked and then the board changes to the next
     * screen.
     */
    public void mainFunction() {
        // TODO Auto-generated method stub
        JOptionPane.showMessageDialog(board,
                "Please select an available property on the screen to buy.");
        landSelection = true;
        board.focusPane();
        while (landSelection) {
            try {
                Thread.sleep(1000);
                if (board.landSelected()) {
                    board.setLandColor(board.getCurrent().getPlayerColor());
                    board.setSelected(false);
                    landSelection = false;
                    board.setlandPhase(false);
                    JOptionPane.showMessageDialog(board,
                            "Land Successfully Purchased!");
                    board.nextCard();
                }
            } catch (Exception e) {
                e.printStackTrace();

            }
        }
        // board.nextCard();
    }

    /**
     * Asks the user to select which property he/she wants to sell. Sells the
     * land and alerts the user.
     */
    public void sellFunction() {
        JOptionPane.showMessageDialog(board,
                "Please select one of your properties to sell.");
        landSelection = true;
        board.focusPane();
        while (landSelection) {
            try {
                Thread.sleep(1000);
                if (board.landSelected()) {
                    board.setSelected(false);
                    landSelection = false;
                    board.setlandPhase(false);
                    JOptionPane.showMessageDialog(board,
                            "Land Successfully Sold!");
                    board.nextCard();
                }
            } catch (Exception e) {
                e.printStackTrace();

            }
        }
    }

}
